package com.github.appintro;

import com.github.appintro.utile.ShapeUtil;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.utils.PacMap;

public abstract class AppIntroBaseFragment extends DirectionalLayout implements SlideBackgroundColorHolder, SlideSelectionListener {
    public static final String ARG_TITLE = "title";
    public static final String ARG_TITLE_TYPEFACE = "title_typeface";
    public static final String ARG_TITLE_TYPEFACE_RES = "title_typeface_res";
    public static final String ARG_DESC = "desc";
    public static final String ARG_DESC_TYPEFACE = "desc_typeface";
    public static final String ARG_DESC_TYPEFACE_RES = "desc_typeface_res";
    public static final String ARG_DRAWABLE = "drawable";
    public static final String ARG_BG_COLOR = "bg_color";
    public static final String ARG_TITLE_COLOR = "title_color";
    public static final String ARG_DESC_COLOR = "desc_color";
    public static final String ARG_BG_DRAWABLE = "bg_drawable";
    private int drawable;
    private int[] bgDrawable;
    private int titleColor;
    private int descColor;
    int defaultBackgroundColor;
    private String title;
    private String description;
    private Component component;
    private Text titleText;
    private Text descriptionText;
    private Image slideImage;
    private DirectionalLayout mainLayout;

    public AppIntroBaseFragment(Context context) {
        this(context, null);
    }

    public AppIntroBaseFragment(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public AppIntroBaseFragment(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_appintro_fragment_intro, this, true);
        titleText = (Text) component.findComponentById(ResourceTable.Id_title);
        descriptionText = (Text) component.findComponentById(ResourceTable.Id_description);
        slideImage = (Image) component.findComponentById(ResourceTable.Id_image);
        mainLayout = (DirectionalLayout) component.findComponentById(ResourceTable.Id_main);
    }

    public void setArguments(PacMap bundles) {
        if (bundles != null && !bundles.isEmpty()) {
            drawable = bundles.getIntValue(ARG_DRAWABLE);
            title = bundles.getString(ARG_TITLE);
            description = bundles.getString(ARG_DESC);
            bgDrawable = bundles.getIntValueArray(ARG_BG_DRAWABLE);
            defaultBackgroundColor = bundles.getIntValue(ARG_BG_COLOR);
            titleColor = bundles.getIntValue(ARG_TITLE_COLOR, 0);
            descColor = bundles.getIntValue(ARG_DESC_COLOR, 0);
        }
        titleText.setText(title);
        descriptionText.setText(description);
        if (this.titleColor != 0) {
            titleText.setTextColor(new Color(titleColor));
        }
        if (this.descColor != 0) {
            descriptionText.setTextColor(new Color(descColor));
        }
        slideImage.setPixelMap(drawable);
        if (this.bgDrawable != null && bgDrawable.length > 0) {
            if (mainLayout != null) {
                RgbColor[] rgbColors = new RgbColor[bgDrawable.length];
                for (int i = 0; i < bgDrawable.length; i++) {
                    rgbColors[i] = new RgbColor(bgDrawable[i]);
                }
                mainLayout.setBackground(ShapeUtil.createDrawable(rgbColors));
            }
        } else if (mainLayout != null) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(defaultBackgroundColor));
            mainLayout.setBackground(shapeElement);
        }
        invalidate();
    }

    @Override
    public void onSlideDeselected() {
    }

    @Override
    public void onSlideSelected() {
    }

    @Override
    public void setBackgroundColor(int backgroundColor) {
        DirectionalLayout view = (DirectionalLayout) findComponentById(ResourceTable.Id_main);
        if (view != null) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(backgroundColor));
            view.setBackground(shapeElement);
        }
    }
}
